package com.cjzuji.rental.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cjzuji.rental.Bean.YoupinRequest;
import com.cjzuji.rental.Config.YpConfig;
import com.cjzuji.rental.controller.order.YouPinController;
import com.cjzuji.rental.dao.*;
import com.cjzuji.rental.mapper.OrderBillMapper;
import com.cjzuji.rental.mapper.OrderMapper;
import com.cjzuji.rental.mapper.YpRiskMapper;
import com.cjzuji.rental.pojo.Orders;
import com.cjzuji.rental.pojo.YpRisk;
import com.cjzuji.rental.service.YouPinService;
import com.cjzuji.rental.service.YpRiskService;
import com.cjzuji.rental.util.DateUtil;
import com.cjzuji.rental.util.Tool;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class YpRiskServiceImpl implements YpRiskService {
    @Autowired
    YpConfig ypConfig;
    @Autowired
    YpRiskDao YpRiskDao;
    @Autowired
    YpRiskMapper YpRiskMapper;
    private static final Logger logger = LoggerFactory.getLogger(YpRiskServiceImpl.class);
    @Override
    //http://127.0.0.1:9099/order/youping/getListInfo
    public List<Map<String, Object>> getOrderssList(String accesstoken, String authorization) {
        String url = ypConfig.getGetListUrl();
        YoupinRequest youpinRequest = new YoupinRequest();
        List<Map<String, Object>> listData = new ArrayList<>();
        youpinRequest.setModel("transferAllList");
        youpinRequest.setPageNo(0);
        youpinRequest.setPageSize(20);
        youpinRequest.setTransferMerchantTag(2);
        youpinRequest.setEndTime(DateUtil.getTodayEnd());
        youpinRequest.setStartTime(DateUtil.getTodayStart());
        int pageNum = 0;  // 从第一页开始
        // 2. 转换为 JSON
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectMapper resMapper = new ObjectMapper();
        ObjectMapper resUserMapper = new ObjectMapper();
        while (true) {
            try {
                youpinRequest.setPageNo(pageNum);
                String json = objectMapper.writeValueAsString(youpinRequest);
                String resStr = Tool.request(url,accesstoken,authorization,json,"", "");
                //logger.info(resStr);
                Map<String,Object> res = resMapper.readValue(resStr,Map.class);
                // 获取 data → list
                Map<String, Object> dataMap = (Map<String, Object>) res.get("data");
                //logger.info("dataMap~~ {}",dataMap);
                List<Map<String, Object>> list = (List<Map<String, Object>>) dataMap.get("list");
                //logger.info("list~~~ {}",list);
                if (list != null && list.size() > 0) {
                    for (Map<String, Object> item : list) {
                        //获取用户信息
                        String userUrl = ypConfig.getGetUserUrl();
                        List<String> userParam = new ArrayList<>();
                        userParam.add(item.get("receiverName").toString());  // 收货人名称
                        userParam.add(item.get("receiverMobile").toString());  //收货人手机
                        userParam.add(item.get("receiverAddressFormat").toString());  // 收货地址
                        userParam.add(item.get("mobile").toString());  //
                        userParam.add(item.get("idcardNo").toString());  //
                        userParam.add(null);  //
                        //logger.info("打印用户信息请求参数 {}",userParam);
                        //将参数转json
                        String userParamStr = objectMapper.writeValueAsString(userParam);
                        //logger.info("打印用户信息请求参数str {}",userParamStr);
                        String userRes =  Tool.request(userUrl,authorization,authorization,userParamStr,item.get("no").toString(), "");
                        Map<String,Object> resUser =  resUserMapper.readValue(userRes,Map.class);
                        List<String> userInfo = (List<String>) resUser.get("data");
                        String orderNo = item.get("no").toString();
                        //查询该订单是否已经存在数据库中，存在则跳出，不存在保存
                        QueryWrapper<YpRisk> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("order_id",orderNo);
                        YpRisk ypRisk = YpRiskMapper.selectOne(queryWrapper);
                        if (ypRisk == null) {
                            System.out.println("保存订单" + orderNo);
                            YpRiskDao.save(orderNo,userInfo);
                        }else {
                            System.out.println("订单已存在" + orderNo);
                        }
                    }
                }else {
                    break;
                }
                pageNum ++;
                logger.info("打印当前页码 {}",pageNum);

            } catch (JsonProcessingException e) {
                logger.info(e.getMessage());
                break;
            }
        }
        return listData;
    }
}






